package com.itheima.auth.handle;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.itheima.support.Result;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class MyAccessDeniedHandler implements AccessDeniedHandler {
    @Override
    public void handle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, AccessDeniedException e) throws IOException, ServletException {
        httpServletResponse.setCharacterEncoding("UTF-8");
        Result<String> result=Result.buildError("您没有权限,请联系管理员");
        ObjectMapper objectMapper=new ObjectMapper();
        httpServletResponse.getWriter().write(objectMapper.writeValueAsString(result));
    }
}
